<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>路由基本用法</title>
    <script src="js/vue.js"></script>
    <script src="js/vue-router.min.js"></script>
</head>

<body>
    <div id="app">
        <div>
            <!--使用router-link定义导航,to属性指定连接url-->
            <router-link to="/home">主页</router-link>
            <router-link to="/user">用户</router-link>
        </div>
        <div>
            <!--router-view 用来显示路由内容-->
            <router-view></router-view>
        </div>
        <hr>
        <button type="button" @click="push">添加路由</button>
        <button type="button" @click="replace">替换路由</button>
    </div>

    <template id="user">
        <div>
            <h3>用户登录</h3>
            <ul>
                <li>
                    <router-link to="/user/login?username=limeng&password=123">用户登录</router-link>
                </li>
                <li>
                    <router-link to="/user/regist/lm/1111">用户注册</router-link>
                </li>
            </ul>
            <router-view></router-view>
        </div>
    </template>

    <script>
        // 1. 定义组件
        var Home = {
            template: '<h3>我是主页</h3>'
        }
        var User = {
            template: '#user'
        }
        var Login = {
            template: '<h2>用户登录....{{$route.query}}</h2>'
        }
        var Regist = {
            template: '<h2>用户注册.....{{$route.params}},{{$route.path}}</h2>'
        }
        // 2. 配置路由
        const routes = [
            {
                path: '/home',
                component: Home
            },
            {
                path: '/user',
                component: User,
                children: [
                    {
                        path: 'login',
                        component: Login
                    },
                    {

                        path: 'regist/:username/:password',
                        component: Regist
                    }
                ]
            },
            {
                path: '*',
                redirect: '/home'
            }
        ]

        // 3.创建路由实例
        const router = new VueRouter({
            // mode:'history',  //路由模式
            // routes:routes
            routes  //简写模式
        });

        // 4. 创建根实例并将路由挂载到Vue实例上
        new Vue({
            el: "#app",
            router,  //注入路由
            methods:{
                push(){
                    router.push({path:'/home'})
                },
                replace(){
                    router.replace({path:'/user'})
                }
            }
        })
    </script>
</body>

</html>